<?php

namespace app\api\controller\v1\baoming;

use app\Request;
use think\facade\Db;

class Baoming
{
    public function getBaoMingList()
    {
        $list=  Db::name('games')->where('is_open',1)->select();
        return app('json')->success(compact('list'));
    }

    public function getBaoMingInfo(Request  $request)
    {
        $uid = $request->uid();
        $baomingModel = app()->make(\app\model\crud\Baoming::class);
        $info = $baomingModel->get($uid);
        return app('json')->success(compact('info'));
    }

    /**
     * 报名
     * @param Request $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        $data = $request->post();
        $baomingModel = app()->make(\app\model\crud\Baoming::class);
        $data['uid'] = $request->uid();

        $baomingInfo = $baomingModel::where('uid', $request->uid())->find();

        if (isset($data['id']) || $baomingInfo!=null){
            $id = isset($data['id']) ? $data['id'] : $baomingInfo['id'];
            $baomingModel->update(['id'=>$id],$data);
        }else{
            $baomingModel->save($data);
        }
        return app('json')->success(100001);
    }

    /**
     * 获取报名信息
     * @param Request $request
     * @return \think\Response
     */
    public function getBaoMingInfoByUid(Request $request)
    {
        $uid = $request->uid();
        $baomingModel = app()->make(\app\model\crud\Baoming::class);
        $info = $baomingModel->where('uid', $uid)->find();
        return app('json')->success(compact('info'));
    }

    public function updateBaoMingInfoByUid(Request $request)
    {
        $uid = $request->uid();
        $data = $request->post();
        $baomingModel = app()->make(\app\model\crud\Baoming::class);
        $baomingModel->where('uid', $uid)->update($data);
        return app('json')->success(100001);
    }

    /**
     * 查询单条报名信息
     * @param Request $request
     * @return \think\Response
     */
    public function search(Request $request)
    {
        $gameId = $request->param('game_id', '');
        $mobile = $request->param('mobile', '');
        
        if (empty($gameId) && empty($mobile)) {
            return app('json')->fail('请至少提供一个查询条件（game_id或mobile）');
        }
        
        $where = [];
        if (!empty($gameId)) {
            $where['game_id'] = $gameId;
        }
        if (!empty($mobile)) {
            $where['mobile'] = $mobile;
        }
        
        $baomingDao = app()->make(\app\dao\crud\BaomingDao::class);
        
        // 查询单条记录
        $item = $baomingDao->searchCrudModel($where, ['*'], 'id desc')->find();
        
        if (!$item) {
            return app('json')->fail('未找到匹配的报名记录');
        }
        
        // 格式化数据
        $data = [
            'id' => $item['id'],
            'uid' => $item['uid'],
            'name' => $item['name'],
            'school_card' => $item['school_card'],
            'mobile' => $item['mobile'],
            'game_id' => $item['game_id'],
            'sancang' => $item['sancang'],
            'create_time' => $item['create_time'],
            'update_time' => $item['update_time']
        ];
        
        return app('json')->success(compact('data'));
    }

    /**
     * 根据game_id查询单条报名信息
     * @param Request $request
     * @return \think\Response
     */
    public function searchByGameId(Request $request)
    {
        $gameId = $request->param('game_id');
        
        if (empty($gameId)) {
            return app('json')->fail('game_id不能为空');
        }
        
        $baomingDao = app()->make(\app\dao\crud\BaomingDao::class);
        $item = $baomingDao->searchCrudModel(['game_id' => $gameId], ['*'], 'id desc')->find();
        
        if (!$item) {
            return app('json')->fail('未找到匹配的报名记录');
        }
        
        return app('json')->success(compact('data'));
    }

    /**
     * 根据mobile查询单条报名信息
     * @param Request $request
     * @return \think\Response
     */
    public function searchByMobile(Request $request)
    {
        $mobile = $request->param('mobile');
        
        if (empty($mobile)) {
            return app('json')->fail('mobile不能为空');
        }
        
        $baomingDao = app()->make(\app\dao\crud\BaomingDao::class);
        $item = $baomingDao->searchCrudModel(['mobile' => $mobile], ['*'], 'id desc')->find();
        
        if (!$item) {
            return app('json')->fail('未找到匹配的报名记录');
        }
        
        return app('json')->success(compact('data'));
    }
    
    
}